package com.dhcc.DRGsHosp.facade.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

import javax.inject.Named;

import org.openkoala.koala.commons.InvokeResult;
import org.springframework.beans.factory.annotation.Autowired;

import com.dhcc.DRGsHosp.application.SystemConfigApplication;
import com.dhcc.DRGsHosp.application.YiBaoHomeApplication;
import com.dhcc.DRGsHosp.facade.DrgsIndexCalFacade;
import com.dhcc.DRGsHosp.facade.YiBaoHomeFacade;
import com.dhcc.DRGsHosp.facade.dto.IndexCalDTO;
import com.dhcc.DRGsHosp.infra.util.GlobalData;

/**
 * 
 * @title		:YiBaoHomeFacadeImpl.java
 * @description	: 医保首页
 * @author		:mazhaoyan
 * @date		:2015年12月16日
 * @version     :1.0
 * @copyright   :DHCC 2013-2014
 */
@Named("drgsHospFacadeImpl48")
public class YiBaoHomeFacadeImpl implements YiBaoHomeFacade{
	@Autowired
	private YiBaoHomeApplication yiBaoHomeApplication;
	
	@Autowired
	private DrgsIndexCalFacade drgsIndexCalFacade;
	@Autowired
	private SystemConfigApplication systemConfigApplication;
	/**
	 * 
	 *  @Description：获取基金数据
	 *  @Author LiuHongJun  
	 *  @see com.dhcc.DRGsHosp.facade.YiBaoHomeFacade#getHCInfoHosp(java.lang.String)
	 */
	@Override
	public InvokeResult getHCInfoHosp(String hiType,String hospCode) {
		return InvokeResult.success(yiBaoHomeApplication.getHCInfoHosp(hiType,getinsTypeByFundType(hiType),hospCode));
	}
	/**
	 * 
	 *  getinsTypeByFundType.  
	 *  @Description：根据基金类型获取医保类型
	 *  @Author LiuHongjun
	 *  @param hiType 基金类型
	 *  @return 
	 *  @Date:2017年10月16日下午3:21:39
	 */
	public String getinsTypeByFundType(String hiType) {
		String insType="";
		try {
			insType="(";
				String  cfgValue= GlobalData.sysConfig.get("MED_INS_TYPE");
				if(cfgValue!=null&&cfgValue.split("θ").length>0) {
					for( int i=0;i<cfgValue.split("θ").length;i++) {
						String str=cfgValue.split("θ")[i];
						if(str.split("#").length>0 && hiType.equals(str.split("#")[0])) {
							String insTypeStr=str.split("#")[1];
							if(insTypeStr.length()>0) {
								for(int j=0;j<insTypeStr.split("&").length;j++) {
									insType+="'"+insTypeStr.split("&")[j]+"',";
								}
							}
							break;
						}
					}
				}
			if (insType.lastIndexOf(",") > 0) {
				insType= insType.substring(0, insType.lastIndexOf(","))+"";
			}
			insType+=")";
		}catch(Exception e) {
			e.printStackTrace();
		}
		return insType;
	}
	/**
	 * 
	 *  @Description：科室监测
	 *  @Author LiuHongJun  
	 *  @see com.dhcc.DRGsHosp.facade.YiBaoHomeFacade#depQuotaMonitor()
	 */
	@Override
	public InvokeResult depQuotaMonitor(String hospCode) {
		//获取医保的最大时间
		String maxDate=yiBaoHomeApplication.getMaxDate();
		String asId = drgsIndexCalFacade.getStdID(maxDate,maxDate);
		String groupScheme=drgsIndexCalFacade.getGroupScheme();
		return InvokeResult.success(yiBaoHomeApplication.depQuotaMonitor(maxDate,asId,hospCode,groupScheme));
	}
	
	/**
	 * 
	 *  @Description：历史数据查询
	 *  @Author LiuHongJun  
	 *  @see com.dhcc.DRGsHosp.facade.YiBaoHomeFacade#getHCHistory(java.lang.String)
	 */
	@Override
	public InvokeResult getHCHistory(String hiType,String hospCode) {
		List<Object[]> list = yiBaoHomeApplication.getHCHistory(hiType,getinsTypeByFundType(hiType),hospCode);
		return InvokeResult.success(list);
	}
	/**
	 * 
	 *  @Description：获取医保首页各监测指标
	 *  @Author LiuHongJun  
	 *  @see com.dhcc.DRGsHosp.facade.YiBaoHomeFacade#findHospQuotaData(java.lang.String)
	 */
	@Override
	public InvokeResult findHospQuotaData(String quotaCode,String hospCode) {
		//查询最新的出院病例
		String maxDate = "";
		Map<String,Object> result = new HashMap<>();
		List<Map<String, Object>> resultListAll = new LinkedList<>();
		List<Map<String, Object>> resultListMendis = new LinkedList<>();
		maxDate = yiBaoHomeApplication.getMaxDate();
		if(!"".equals(maxDate) && null!=maxDate){
			int year = Integer.parseInt(maxDate.substring(0,4));
			int mon = Integer.parseInt(maxDate.substring(5,7));
			String dateFrom="";
			String dateTo="";
			String desc = "";
			if(mon==12){
				year=year-1;
				mon=1;
			}else{
				year=year-2;
				mon=mon+1;
			}
			
			List<Future<Map<String, Object>>> listYBf = new ArrayList<>();
			ExecutorService ybPool = Executors.newFixedThreadPool(24);
			
			for (int i = 0; i < 24; i++) {
				if(mon == 13){
					mon = 1;
					year = year+1;
				}
				if(mon<10){
					desc = year+"年0"+mon+"月";
					dateFrom=year+"-0"+mon;
					dateTo=year+"-0"+mon;
				}else {
					desc = year+"年"+mon+"月";
					dateFrom=year+"-"+mon;
					dateTo=year+"-"+mon;
				}
				//医保数据
				IndexCalDTO dtom=new IndexCalDTO();
				dtom.setMonitorType("hosp");
				dtom.setQuotaCode(quotaCode);
				dtom.setDateFrom(dateFrom);
				dtom.setDateTo(dateTo);
				dtom.setpType("1");
				dtom.setHospCode(hospCode);
				yiBaoCallable c = new yiBaoCallable(dtom,desc);
				Future<Map<String, Object>> f = ybPool.submit(c);
				//全院数据
				IndexCalDTO dto=new IndexCalDTO();
				dto.setMonitorType("hosp");
				dto.setQuotaCode(quotaCode);
				dto.setDateFrom(dateFrom);
				dto.setDateTo(dateTo);
				dto.setpType("0");
				dto.setHospCode(hospCode);
				yiBaoCallable ca = new yiBaoCallable(dto,desc);
				Future<Map<String, Object>> fa = ybPool.submit(ca);
				listYBf.add(f);
				listYBf.add(fa);
				mon=mon+1;
			}
			ybPool.shutdown();
			//从future中取出数据
			for (Future<Map<String, Object>> fr : listYBf) {
					try {
						if(fr.get().get("mapa")!=null){
							resultListAll.add((Map<String, Object>) fr.get().get("mapa"));
						}
						if(fr.get().get("mapm")!=null){
							resultListMendis.add((Map<String, Object>) fr.get().get("mapm"));
						}
						
					} catch (InterruptedException e) {
						e.printStackTrace();
					} catch (ExecutionException e) {
						e.printStackTrace();
					}
			}	
			result.put("all",resultListAll);
			result.put("mendis",resultListMendis);
		}
		return InvokeResult.success(result);
	}
	/**
	 * 
	 * @ClassName: yiBaoCallable  
	 * @Description ：线程实现数据获取    
	 * @Date: 2017年8月8日 下午3:49:48  
	 * @Author LiuHongJun 
	 * @Version 2.5.0 DHC-DRGs
	 */
	private class yiBaoCallable implements Callable<Map<String,Object>>{ 
		private IndexCalDTO dto;
		private String desc;
		yiBaoCallable(IndexCalDTO dto,String desc) {
		 this.dto=dto;
		 this.desc=desc;
		} 
        @Override 
        public Map<String,Object> call() {        		
        	Map<String, Object> resmap = new HashMap<>();
        	try {				
				List<Map<String, Object>> list = (List<Map<String, Object>>) drgsIndexCalFacade.indexCal(dto);
				Map<String, Object> map = new HashMap<>();
				if(list.size()>0){
					map = list.get(0);
				}	
				map.put("ddesc",desc);
				if("1".equals(dto.getpType())){
					resmap.put("mapm", map);
				}else if("0".equals(dto.getpType())){
					resmap.put("mapa", map);	
				}			
			} catch (Exception e) {
				e.printStackTrace();
			}
        	return resmap;
        } 
}
	/**
	 *  @Discription：获取医保最大出院时间
	 *  @Author： liuhongjun
	 *  @return
	 *  @Date:2018年7月28日上午9:46:38
	 */
	@Override
	public String getMaxDate() {
		return yiBaoHomeApplication.getMaxDate();
	}

}
